var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var deviceone = require("deviceone");
var circleUrl = require("url/circle");
var http = require("helper/http");

//声明UI变量
var listview=ui("do_SlideListView_1");
var listdata = mm("do_ListData");
listview.bindItems(listdata);//绑定item的数据

// UI
var headerAout = ui("headerAout");
var linearLayout_main = ui("linearLayout_main")

// 加载头部
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
// andriod返回键
page.on("back", function() {
	app.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
// 右上角-
var headerRight = ui(addheader + ".headerRight");
headerRight.visible = false;
headerLeft.on("touch", function() {
	app.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "设置成员头衔",
})
header.refreshData();

/**
 * ********************************************************switch
 */
var addSwitch1 = linearLayout_main.add("switch1","source://view/reuse/form/mutiSwitch.ui");
var switch1 = ui("switch1");
switch1.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "显示成员头衔"
})
switch1.refreshData();
switch1.on("switchtag", function(data) {
	if (data == 0) {// 关闭，不显示
		hideMemberGrade();
	}
	if (data == 1) {// 打开，显示
		showMemberGrade();
	}
});

var circleId = null;
//页面装载完成后，开始初始化工作
page.on("loaded", function(){
	circleId = page.getData().circleId;
	loadPage();
});

page.on("memberGrade_update", function(){
	loadGradeList();
});

//从新增页面返回,刷新数据
page.on("result", function(data){
	if (data.memberGrade_updata) {
		loadGradeList();
    }
});

function loadPage() {
	//var swtxt = [ 0, " ", " " ]; //[开关状态 (0-关;1-开),关闭状态文字 ,开启状态文字]
	http.post(circleUrl.url.base.isShowMemberGrade, {id: circleId}, function(data) {
		if (data.isShowMemberGrade) {
			switch1.fire("switchzt", [ 1, " ", " " ]);
		}else{
			switch1.fire("switchzt", [ 0, " ", " " ]);
		}
	})
	loadGradeList();
}

function loadGradeList(){
	http.post(circleUrl.url.memberGrade.list4circle,{"circleId":circleId},function(data){
		var gradeList = data.gradeList;
		listdata.removeAll();//清除数据
		if(gradeList.length){
			for(var i=0;i<gradeList.length;i++){
				gradeList[i].template=0;
				gradeList[i].rightTemplate=1;
			}
			listdata.addData(gradeList);
			listview.refreshItems();//刷新item数据
		}
	});
}
function showMemberGrade() {
	http.post(circleUrl.url.base.showMemberGrade, {id: circleId}, function(data) {
	})
}
function hideMemberGrade() {
	http.post(circleUrl.url.base.hideMemberGrade, {id: circleId}, function(data) {
	})
}
